package com.ruixun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruixun.common.PageData;
import com.ruixun.entity.SysMenu;
import com.ruixun.entity.SysPermission;
import com.ruixun.mapper.SysPermissionMapper;
import com.ruixun.query.SysPermissionPageQuery;
import com.ruixun.rvo.PermissionStatsRvo;
import com.ruixun.rvo.SysMenuRvo;
import com.ruixun.rvo.SysPermissionRvo;
import com.ruixun.service.SysPermissionService;
import com.ruixun.utils.PageUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 权限服务实现类
 * @author ruixun
 */
@Service
@RequiredArgsConstructor
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysPermission> implements SysPermissionService {

    private final SysPermissionMapper permissionMapper;

    @Override
    public PageData<SysPermissionRvo> getPermissionPage(SysPermissionPageQuery query) {
        // 构建分页对象
        Page<SysPermission> page = new Page<>(query.getPageNum(), query.getPageSize());

        // 构建查询条件
        LambdaQueryWrapper<SysPermission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysPermission::getDeleted, 0); // 未删除的数据

        // 权限名称模糊查询
        if (StringUtils.hasText(query.getPermissionName())) {
            wrapper.like(SysPermission::getPermissionName, query.getPermissionName());
        }

        // 权限类型等值查询
        if (query.getPermissionType() != null) {
            wrapper.eq(SysPermission::getPermissionType, query.getPermissionType());
        }

        // 排序（按创建时间倒序）
        wrapper.orderByDesc(SysPermission::getCreateTime);

        // 执行分页查询
        IPage<SysPermission> permissionPage = permissionMapper.selectPage(page, wrapper);

        // 转换结果
        List<SysPermissionRvo> rvoList = permissionPage.getRecords().stream()
                .map(this::convertToRvo)
                .collect(Collectors.toList());

        return new PageUtils<SysPermissionRvo>().convert( permissionPage.getCurrent(), permissionPage.getSize(), permissionPage.getTotal(),rvoList);
    }

    @Override
    public boolean addPermission(SysPermission permission) {
        return save(permission);
    }

    @Override
    public boolean updatePermission(SysPermission permission) {
        return updateById(permission);
    }

    @Override
    public boolean deletePermission(Long id) {
        SysPermission permission = getById(id);
        if (permission != null) {
            permission.setDeleted(1); // 逻辑删除
            return updateById(permission);
        }
        return false;
    }

    @Override
    public SysPermissionRvo getPermissionById(Long id) {
        SysPermission permission = getById(id);
        return permission != null ? convertToRvo(permission) : null;
    }

    /**
     * 实体类转换为返回视图对象
     * @param permission 权限实体
     * @return 返回视图对象
     */
    private SysPermissionRvo convertToRvo(SysPermission permission) {
        SysPermissionRvo rvo = new SysPermissionRvo();
        BeanUtils.copyProperties(permission, rvo);
        return rvo;
    }

    @Override
    public List<SysPermissionRvo> userMenu() {
        //获取当前登录用户ID,查询当前登录用户对应的权限
        LambdaQueryWrapper<SysPermission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysPermission::getDeleted, 0);
        List<SysPermission> sysPermissions = permissionMapper.selectList(wrapper);
        List<SysPermissionRvo> dataList = sysPermissions.stream().map(this::convertToRvo).collect(Collectors.toList());
        return dataList;
    }

    @Override
    public PermissionStatsRvo stats() {
        PermissionStatsRvo rvo = new PermissionStatsRvo();

        LambdaQueryWrapper<SysPermission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysPermission::getDeleted,0);
        Long count = permissionMapper.selectCount(wrapper);
        wrapper.eq(SysPermission::getPermissionType,0);
        Long count1 = permissionMapper.selectCount(wrapper);
        wrapper.eq(SysPermission::getPermissionType,1);
        Long count2 = permissionMapper.selectCount(wrapper);
        rvo.setTotal(Math.toIntExact(count));
        rvo.setMenuCount(Math.toIntExact(count1));
        rvo.setButtonCount(Math.toIntExact(count2));
        return rvo;
    }

    @Override
    public List<SysPermissionRvo> all() {
        LambdaQueryWrapper<SysPermission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysPermission::getDeleted, 0); // 未删除的数据
        List<SysPermission> sysPermissionList = permissionMapper.selectList(wrapper);
        List<SysPermissionRvo> rvoList = sysPermissionList.stream()
                .map(this::convertToRvo)
                .collect(Collectors.toList());
        return rvoList;
    }
}